
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO
Imports _2bMatesSite.Common


Namespace _2bMatesSite
	''' <summary>
	''' Summary description for upload_photo.
	''' </summary>
	Public Partial Class add_photo_description
		Inherits BaseWebPage
		Protected PicturesTable As System.Web.UI.WebControls.Table
		Protected lblNoImage As System.Web.UI.WebControls.Label
		Protected Const MAX_FILE_SIZE As Integer = 512000
		' in bytes
		Private imgID As Integer = -1
		Private grpID As Integer = -1
		Private isGroupMode As Boolean = False
		Private grp As Group = Nothing

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' An example displaying the first picture
			' this.pageTemplate.PageTitle = PageTitles.ADD_PHOTO_DESCRIPTION;
			Dim usr As User = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			' init page from XML
			Me.initPageTitles(Constants.ADD_PHOTO_DESCRIPTION, "", uploadImageForm, Nothing, Nothing)
			Me.initAdvertisements(uploadImageForm, Nothing)

			If Request.Params("id") = Nothing Then
				SetVisibilityToError()
			Else
				imgID = Convert.ToInt32(Request.Params("id"))

				' init group
				If Request.Params(Constants.GROUP_PARAM) <> Nothing AndAlso Request.Params(Constants.GROUP_PARAM) <> "" Then
					isGroupMode = True
					grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
					grp = Groups.GetGroupByID(grpID)
					If grp = Nothing Then
						Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
					End If
				End If

				If Not IsPostBack Then
					' init photo
					If isGroupMode Then
						Dim pic As GroupPicture = grp.GetPicture(imgID)
						imgPhotoBig.ImageUrl = Pictures.GetPictureSrcFullURL(pic, False, "../")
						txtDescription.Text = pic.Desc
						If pic.Desc <> "" Then
							litAddEdit.Text = "Edit"
							btnAdd.Text = "Update Description"
							btnUpdate.Visible = True
							btnAdd.Visible = False
							btnAdd.Width = 150
						End If
						hlBackToGroup.NavigateUrl = "../" + Constants.GROUP_WELCOME + "?" + Constants.GROUP_PARAM + "=" + grpID
						hlBackToGroup.Text = Constants.GROUP_BACK_LINK + grp.Name
					Else
						Dim pic As UserPicture = usr.GetPicture(imgID)
						imgPhotoBig.ImageUrl = Pictures.GetPictureSrcFullURL(pic, False, "../")
						txtDescription.Text = pic.Desc
						If pic.Desc <> "" Then
							litAddEdit.Text = "Edit"
							btnUpdate.Visible = True
							btnAdd.Visible = False
							btnAdd.Width = 150
						End If
					End If
				End If
			End If
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()
			AddHandler Me.valFileFormat.ServerValidate, AddressOf Me.validateFile
			AddHandler Me.valFileSize.ServerValidate, AddressOf Me.validateFileSize

		End Sub
#End Region

		Protected Sub validateFile(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
			Dim fileName As String = args.Value.Trim()
			Dim fileNameInLowerCase As String = fileName.ToLower()
			Dim fileSize As Integer = fldFileUpload.PostedFile.ContentLength

			If fileNameInLowerCase.EndsWith(".gif") OrElse fileNameInLowerCase.EndsWith(".jpg") OrElse fileNameInLowerCase.EndsWith(".png") Then
				args.IsValid = True
			Else
				args.IsValid = False
			End If
		End Sub

		Protected Sub validateFileSize(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
			Dim fileName As String = args.Value.Trim()
			Dim fileNameInLowerCase As String = fileName.ToLower()
			Dim fileSize As Integer = fldFileUpload.PostedFile.ContentLength

			If fileSize > MAX_FILE_SIZE Then
				args.IsValid = False
			Else
				args.IsValid = True
			End If
		End Sub

		Protected Sub UploadImage(ByVal file As HttpPostedFile)
			Dim splitFileName As String() = file.FileName.Split(New Char() {"/"C, "\"C})

			If isGroupMode Then
				grp.AddPicture(file.InputStream, splitFileName(splitFileName.Length - 1), "")
			Else
				LoggedUser.AddPicture(file.InputStream, splitFileName(splitFileName.Length - 1), "")
			End If
		End Sub

		Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Page.Validate()
			If Page.IsValid Then
				If isGroupMode Then
					UploadImage(fldFileUpload.PostedFile)
					If grp.DefaultPicture = Nothing Then
						Dim pics As ArrayList = grp.GetPictures()
						grp.DefaultPictureID = (DirectCast(pics(0), GroupPicture)).ID
						grp.Save()
					End If
				Else
					Dim usr As BO.User = LoggedUser
					UploadImage(fldFileUpload.PostedFile)
					If usr.DefaultPicture = Nothing Then
						Dim pics As ArrayList = usr.GetPictures()
						usr.DefaultPictureID = (DirectCast(pics(0), UserPicture)).ID
						usr.Save()
					End If
				End If
				Response.Redirect(Constants.UPLOAD_PHOTO)
			End If
		End Sub

		Protected Sub SetVisibilityToError()
			imgPhotoBig.Visible = False
			txtDescription.Visible = False
			btnAdd.Visible = False
			lblErrorMessage.Visible = True
		End Sub

		Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			Dim dt As DateTime = DateTime.Now.AddMinutes(20)

			Dim temp As String = ""
			temp = txtDescription.Text.Trim()

			'HttpCookie desc = new HttpCookie("desc", temp.Substring(0, Math.Min(temp.Length, Constants.TEXT_FIELD_DEFAULT_MAX_CHARS)));
			'desc.Expires = dt;
			'Response.Cookies.Add(desc);
			If isGroupMode Then
				Response.Redirect(Constants.CONFIRM_PHOTO_DESCRIPTION + "?id=" + Request.Params("id") + "&usr=" + LoggedUser.ID + "&" + Constants.GROUP_PARAM + "=" + grpID + "&desc=" + Server.UrlEncode(temp))
			Else
				Response.Redirect(Constants.CONFIRM_PHOTO_DESCRIPTION + "?id=" + Request.Params("id") + "&usr=" + LoggedUser.ID + "&desc=" + Server.UrlEncode(temp))
			End If
		End Sub

		====================
        ' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable uploadImageForm.
		====================
        Public ReadOnly Property uploadImageForm() As System.Web.UI.HtmlControls.HtmlForm
            Get
                Return Migrated_uploadImageForm
            End Get
        End Property
        'set { Migrated_uploadImageForm = value; }
    End Class
End Namespace






